﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using NutriStat.Data;

namespace NutriStat
{
    public partial class NewTableDialog : OpenTableDialogBase
    {
        #region Data
        private bool instantTableCreation;
        #endregion Data
        /// <summary>
        /// Constructor
        /// </summary>
        public NewTableDialog() : base()
        {
            InitializeComponent();            
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="callingMenu">The form that opened this form</param>
        public NewTableDialog(FormBase callingMenu) : base(callingMenu)
        {
            InitializeComponent();
            InstantTableCreation = true;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="callingMenu">The form that opened this form</param>
        public NewTableDialog(FormBase callingMenu, bool instantlyCreateTable)
            : base(callingMenu)
        {            
            InitializeComponent();
            InstantTableCreation = instantlyCreateTable;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="callingMenu">The form that opened this form</param>
        public NewTableDialog(FormBase callingMenu, NutriStat.Data.IDbDriver database, DataTable table)
            : base(callingMenu, database, table)
        {
            InitializeComponent();
            InstantTableCreation = true;
        }

        /// <summary>
        /// Destructor
        /// </summary>        
        ~NewTableDialog()
        {
            menu.UseWaitCursor = false;
            menu.Enabled = true;
        }

        private void NewTableDialog_FormClosed(object sender, FormClosedEventArgs e)
        {
            menu.UseWaitCursor = false;
            menu.Enabled = true;
            // This must be re-set to the program's default, otherwise the program will be looking
            // for the reference MDBs inside the directory in which the file was just opened.
            Directory.SetCurrentDirectory(NutriConfig.GetCurrentDirectory()); 
        }

        /// <summary>
        /// Gets/sets whether or not to instantly create the table when the user clicks OK. If
        /// set to false, it is assumed that the calling menu will handle the creation at some
        /// later date and only needs to know the name of the table.
        /// </summary>
        public bool InstantTableCreation
        {
            get
            {
                return instantTableCreation;
            }
            set
            {
                instantTableCreation = value;
            }
        }
        /*
        private void textBoxTableName_KeyPress(object sender, KeyPressEventArgs e)
        {
            base.OnKeyPress(e);            

            string keyInput = e.KeyChar.ToString();

            if (Char.IsLetterOrDigit(e.KeyChar) || Char.IsControl(e.KeyChar))
            {
                // Digits and letters are okay
            }            
            else
            {
                // Consume this invalid key
                e.Handled = true;
            }
        }
        */
        private void buttonOK_Click_1(object sender, EventArgs e)
        {
            string tableName = textBoxTableName.Text;            

            if (tableName.Equals(string.Empty))
            {
                MessageBox.Show(this, SharedStrings.WARNING_BLANK_TABLE_NAME, SharedStrings.WARNING, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }

            tableName = "nutri" + tableName;

            bool success = false;

            if (InstantTableCreation)
            {
                success = CreateTable(tableName);
                if (!success)
                {
                    MessageBox.Show(this, SharedStrings.ERROR_TABLE_CREATION1, SharedStrings.ERROR, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                else
                {
                    dt = db.GetTableData(tableName, "*", "fldIdNo, fldCurrentDate ASC");
                }
            }            
            
            dt.TableName = tableName;

            menu.GuiMediator().Database = db;
            menu.GuiMediator().Table = dt;
            menu.GuiMediator().ConnectionString = db.ConnectionString;
            menu.OpenDatabase();
            this.Close();
            
        }

        private void NewTableDialog_Load(object sender, EventArgs e)
        {
            //listBoxTables.Enabled = false;
            listBoxTables.SelectionMode = SelectionMode.None;
            SetCheckFileExists(false);
            AllowFileCreation = true;            

            ConfigureFormControls();
        }

        #region Methods
        /// <summary>
        /// Configures the form controls based on the user's preferences and regional settings.
        /// </summary>
        protected internal override void ConfigureFormControls()
        {
 	        base.ConfigureFormControls();    
            this.Text = SharedStrings.NEW_TABLE;
            labelTableName.Text = SharedStrings.NEW_TABLE_NAME;
        }
        #endregion Methods
    }
}